﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace NCKH.Admin
{
    public partial class Hoidong_Sua : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                if (Request.QueryString["pid"] != null)
                {
                    int hoidongID = Convert.ToInt32(Request.QueryString["pid"]);
                    hienhoidong(hoidongID);

                }
        }

        private void hienhoidong(int hoidongID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlSelect = "SELECT MaHiD,TenHiD, Soquyetdinh ,Mota, Trangthai"
                + " FROM HOIDONG"
                + " WHERE MaHiD=" + hoidongID;//de phong Sql INJECTION
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            txtTenHD.Text = rd["TenHiD"].ToString();
                            txtquyetdinh.Text = rd["Soquyetdinh"].ToString();
                            txtmota.Text = rd["Mota"].ToString();
                            ddlTrangthai.SelectedValue = rd["Trangthai"].ToString();
                            ibtSua.CommandArgument = hoidongID.ToString();
                        }
                        rd.Close();
                    }
                    Cnn.Close();
                }
            }
        }
        protected void ibtHuy_Click(object sender, EventArgs e)
        {
            txtTenHD.Text =
            txtquyetdinh.Text =
            txtmota.Text =
            string.Empty;
            txtTenHD.Focus();
            Response.Redirect("Hoidong.aspx");
        }

        protected void ibtSua_Click(object sender, EventArgs e)
        {

            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Sua_hoidong", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaHiD", ibtSua.CommandArgument);
                    Cmd.Parameters.AddWithValue("@TenHiD", txtTenHD.Text);
                    Cmd.Parameters.AddWithValue("@Soquyetdinh", txtquyetdinh.Text);
                    Cmd.Parameters.AddWithValue("@Mota", txtmota.Text);
                    Cmd.Parameters.AddWithValue("@Trangthai", ddlTrangthai.SelectedValue);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
            Response.Redirect("Hoidong.aspx");
        }

    }
}